package com.kuanter.kuanterauto.activity;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.baseui.BaseActivity_NoBar;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.constants.HttpConfigAPI;
import com.kuanter.kuanterauto.constants.MainConstants;
import com.kuanter.kuanterauto.entity.MerchanDetailService;
import com.kuanter.kuanterauto.entity.MerchanInfo;
import com.kuanter.kuanterauto.entity.MerchanServiceInfo;
import com.kuanter.kuanterauto.entity.SiteInfo;
import com.kuanter.kuanterauto.net.HttpTask;
import com.kuanter.kuanterauto.net.HttpUtil;
import com.kuanter.kuanterauto.utils.AnimationController;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.kuanter.kuanterauto.utils.DimensUtil;
import com.kuanter.kuanterauto.utils.JsonParser;
import com.kuanter.kuanterauto.wxapi.WXEntryActivity;
import com.umeng.analytics.MobclickAgent;

/**
 * 网点详情
 * @author kuanter
 *
 */
@Deprecated
public class MerchantTurnActivity1 extends BaseActivity_NoBar {
	
	ImageView merchan_image;  //商户图片展示
	RelativeLayout bt_webSite_call;  //打电话给商户
	RelativeLayout bt_webSite_getRoute;  //用户位置到商户路线
	
	boolean localCity;
	
	String typeID = "";
	String serviceID = "";
	String id;  //商户ID
	int position;  //商户在列表中的位置
	SiteInfo current;  //当前商户的信息
	
	//附近的第一个商户
	SiteInfo first;
	RelativeLayout close_first;
	int pre_position;
	
	//附近的第二个商户
	SiteInfo second;
	RelativeLayout close_second;
	int next_position;
	
	TextView kuanter_middle; //网点名字
	
	
	MerchanInfo info; //商户详情
	
	private String currentService = "";
	private String currentJifen = "";
	private String merchanImg = "";
	
	public static boolean sh = false;
	
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if(progress != null) {
				progress.setVisibility(View.GONE);
			}
			if(MerchantTurnActivity1.this != null && !MerchantTurnActivity1.this.isFinishing()) {
//				finishProgress();
			}
			if(msg.what == HttpTask.RESULT_OK) {
				String result = (String) msg.obj;
				//json解析
				info = JsonParser.getMerchanInfo(result, ProApplication.getInstance().getList_kuanterService(), serviceID);
				
				merchanImg = info.getMerchanImgUrl();
				
				if(info != null) {
					new DownLoadImage(281,180,merchan_image).execute(info.getMerchanImgUrl());
					if(info.getQueueingProbability().equals("1")) {
						paidui_possible.setText("低");
						paidui_possible.setTextColor(Color.parseColor("#3cc800"));
					} else if(info.getQueueingProbability().equals("2")) {
						paidui_possible.setText("中");
						paidui_possible.setTextColor(Color.parseColor("#ff8c00"));
					} else if(info.getQueueingProbability().equals("3")) {
						paidui_possible.setText("高");
						paidui_possible.setTextColor(Color.parseColor("#ff0000"));
					}
					tv_webSite_phone.setText(info.getMerchanPhone());
					tv_webSite_address.setText(info.getMerchanAddress());
					
					MerchanServiceInfo current = info.getCurrentService();
					
					if(current != null) {
						currentService = current.getServiceName();  //TODO
						
						current_service_name.setText(current.getServiceName());
						List<MerchanDetailService> detais = current.getDetails();
						for(int i = 0; i < detais.size(); i++) {
							RelativeLayout view = (RelativeLayout) factory.inflate(R.layout.merchan_detail_service, null);
							TextView tv_merchan_detail_name = (TextView) view.findViewById(R.id.tv_merchan_detail_name);
							TextView tv_merchan_detail_price = (TextView) view.findViewById(R.id.tv_merchan_detail_price);
							TextView tv_merchan_detail_jf = (TextView) view.findViewById(R.id.tv_merchan_detail_jf);
							tv_merchan_detail_name.setText(detais.get(i).getDetailName());
							tv_merchan_detail_price.setText("￥"+detais.get(i).getDetailPrice());
							tv_merchan_detail_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
							tv_merchan_detail_jf.setText(detais.get(i).getDetailIntegral() + "  积分");
							
							LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
							lp.setMargins(0, 0, 0, 20);
							
							current_detail.addView(view,lp);
						}
					}
					
					List<MerchanServiceInfo> other_service = info.getList_other_service();
					for(int i = 0; i < other_service.size(); i++) {
						MerchanServiceInfo other = other_service.get(i);
						ViewGroup serviceView = (ViewGroup) factory.inflate(R.layout.merchan_service_expand, null);
						TextView tv_merchan_service = (TextView) serviceView.findViewById(R.id.tv_merchan_service);
						tv_merchan_service.setText(other.getServiceName());
						
						LinearLayout linear_merchan_detail_service = (LinearLayout) serviceView.findViewById(R.id.linear_merchan_detail_service);
						List<MerchanDetailService> list_details = other.getDetails();
						for(int j = 0; j < list_details.size(); j++) {
							View other_view = factory.inflate(R.layout.merchan_detail_service, null);
							TextView tv_merchan_detail_name = (TextView) other_view.findViewById(R.id.tv_merchan_detail_name);
							TextView tv_merchan_detail_price = (TextView) other_view.findViewById(R.id.tv_merchan_detail_price);
							TextView tv_merchan_detail_jf = (TextView) other_view.findViewById(R.id.tv_merchan_detail_jf);
							tv_merchan_detail_name.setText(list_details.get(j).getDetailName());
							tv_merchan_detail_price.setText("￥"+list_details.get(j).getDetailPrice());
							tv_merchan_detail_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
							tv_merchan_detail_jf.setText(list_details.get(j).getDetailIntegral() + "  积分");
							
							LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
							lp.setMargins(0, 0, 0, 20);
							
							linear_merchan_detail_service.addView(other_view,lp);
						}
						
						//serviceView  设置padding
						int px = DimensUtil.dipTopx(MerchantTurnActivity1.this, 20);
						//当前服务的名字设置的是距离top 20dp，而detail设置的是20px。所以把相差的值补上  
						int topDistance = px - 20;
						serviceView.setPadding(px, topDistance, px, 0);
						MerchantTurnActivity1.this.other_service.addView(serviceView);
					}
					
				}
				
				if(info.getList_other_service() == null || info.getList_other_service().size() == 0) {
					scan_more.setVisibility(View.GONE);
				} else {
					scan_more.setVisibility(View.VISIBLE);
				}
			} else {
				showToast("加载数据失败");
			}
			super.handleMessage(msg);
		}
		
	};
	
	ScrollView scrollView;
	
	ImageView imageView33;   //第一个分割线
	ImageView imageView34;   //第二个分割线
	
	TextView merchanName;   //商户名称
	RatingBar mer_rate_pf;	//商户图片
	TextView paidui_possible;	//排队可能性
	TextView tv_webSite_address;  //商户地址
	TextView tv_webSite_phone;  //商户电话
	
	LinearLayout current_service;
	TextView current_service_name;
	LinearLayout current_detail;
	
	RelativeLayout scan_more;
	LinearLayout other_service;
	
	LayoutInflater factory;
	
	Button kuanter_right;
	
	boolean other_visiable = false;
	ImageView more_or_less;
	String score;
	
	TextView tv_other_site;
	ImageView image_line32;
	
	LinearLayout progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.kuanter_merchan);
		
		factory = getLayoutInflater();
//		showProgress("正在加载数据...");
		
		progress = (LinearLayout) findViewById(R.id.progress);
		
		scrollView = (ScrollView) findViewById(R.id.scrollView);
		close_first = (RelativeLayout) findViewById(R.id.close_1);
		close_first.setBackgroundResource(R.drawable.websitedetailinfo);
		close_first.setClickable(true);
		
		close_second = (RelativeLayout) findViewById(R.id.close_2);
		close_second.setClickable(true);
		close_second.setBackgroundResource(R.drawable.websitedetailinfo);
		
		imageView33 = (ImageView) findViewById(R.id.imageView33);
		imageView34 = (ImageView) findViewById(R.id.imageView34);
		
		tv_other_site = (TextView) findViewById(R.id.tv_other_site);
		image_line32 = (ImageView) findViewById(R.id.imageView32);
		
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		
		Intent intent = getIntent();
		//得到当前网点的ID
		id = intent.getStringExtra("siteID");
		//服务分类ID
		typeID = intent.getStringExtra("typeID");
		//具体服务ID
		serviceID = intent.getStringExtra("serviceID");
		//当前网点的位置
		position = intent.getIntExtra("sitePosition", -1);
		//当前网点的数据
		if(ServiceTurnOver1.getCurrentData().size() > 0) {
		
		current = ServiceTurnOver1.getCurrentData().get(position);
		currentJifen = current.getMinServicePrice() + "-" + current.getMaxServicePrice() + " 积分";
		
		
		score = current.getScore();
		
		localCity = intent.getBooleanExtra("localCity", false);
		
		//计算出附近的两个网点及他们的位置
		if(position >= 0) {
			if(ServiceTurnOver1.getCurrentData().size() > 2) {
				
				if(position == ServiceTurnOver1.getCurrentData().size() -1 ) {  //最后一个数据
					first = ServiceTurnOver1.getCurrentData().get(position - 2);
					pre_position = position - 2;
					
					second = ServiceTurnOver1.getCurrentData().get(position - 1);
					next_position = position - 1;
				} else if(position == ServiceTurnOver1.getCurrentData().size() - 2) {  //倒数第二个
					first = ServiceTurnOver1.getCurrentData().get(position - 1);
					pre_position = position - 1;
					
					second = ServiceTurnOver1.getCurrentData().get(position + 1);
					next_position = position + 1;
				} else {
					first = ServiceTurnOver1.getCurrentData().get(position + 1);
					pre_position = position + 1;
					
					second = ServiceTurnOver1.getCurrentData().get(position + 2);
					next_position = position + 2;
				}
			} else if(ServiceTurnOver1.getCurrentData().size() == 2) {
				if(position == 0) {
					next_position = 1;
					second = ServiceTurnOver1.getCurrentData().get(1);
					close_first.setVisibility(View.GONE);
					imageView33.setVisibility(View.GONE);
				} else if(position == 1) {
					pre_position = 0;
					first = ServiceTurnOver1.getCurrentData().get(0);
					close_second.setVisibility(View.GONE);
					imageView34.setVisibility(View.GONE);
				}
			} else {
				imageView33.setVisibility(View.GONE);
				imageView34.setVisibility(View.GONE);
				
				close_first.setVisibility(View.GONE);
				close_second.setVisibility(View.GONE);
			}
			
		}
		
		initView();
		//根据ID发送数据，得到网点的详情
		if(id != null && !id.equals("")) {
			//TODO 请求数据
			DataLoader loader = new DataLoader(this);
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			NameValuePair param = new BasicNameValuePair("shopId", id);
			params.add(param);
			loader.DisplayData(MainConstants.HOST_test + HttpConfigAPI.FINDMERCHAN, params, mHandler, "", false);
		}
		
		
		} else {
			/*Toast.makeText(this, "获取数据失败", Toast.LENGTH_SHORT).show();
			progress.setVisibility(View.GONE);*/
//			finish();
		}
		
		
		
	}

	private void initView() {
		//商户名称，根据传递进来的数据进行赋值   TODO
		merchanName = (TextView) findViewById(R.id.merchanName);
		merchanName.setText(ServiceTurnOver1.getCurrentData().get(position).getSiteName());
		
		//网点详情
		kuanter_middle = (TextView) findViewById(R.id.kuanter_middle);
		kuanter_middle.setText("网点详情");
		
		kuanter_right = (Button) findViewById(R.id.kuanter_right);
		kuanter_right.setVisibility(View.VISIBLE);
		kuanter_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Builder build = new AlertDialog.Builder(MerchantTurnActivity1.this).setTitle("分享好友")
				.setItems(R.array.share_item, new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						if(which == 0) {
//							Intent intent = new Intent(Intent.ACTION_MAIN);
//							intent.addCategory(Intent.CATEGORY_DEFAULT);
//							intent.setType("vnd.android-dir/mms-sms");
							Uri uri = Uri.parse("smsto:");
							Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
							intent.putExtra("sms_body", "我在宽途网发现一家不错的店，快来看看吧：" + merchanName.getText().toString() + ";\n" + "地址：" + tv_webSite_address.getText().toString()
									+ ";\n" + "电话：" + tv_webSite_phone.getText().toString() + ";\n");
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 1) {
							String phone = tv_webSite_phone.getText().toString();
							String address = tv_webSite_address.getText().toString();
							String name = merchanName.getText().toString();
							Intent intent = new Intent(MerchantTurnActivity1.this, WXEntryActivity.class);
							intent.putExtra("phone", phone);
							intent.putExtra("address", address);
							intent.putExtra("name", name);
							intent.putExtra("currentService", currentService);
							intent.putExtra("currentJifen", currentJifen);
							intent.putExtra("merchanImg", merchanImg);
							intent.putExtra("status", 2);
							intent.putExtra("friend", false);
							intent.putExtra("who", ShareActivity.MY);
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						}else if(which == 2) {
							String phone = tv_webSite_phone.getText().toString();
							String address = tv_webSite_address.getText().toString();
							String name = merchanName.getText().toString();
							Intent intent = new Intent(MerchantTurnActivity1.this, WXEntryActivity.class);
							intent.putExtra("phone", phone);
							intent.putExtra("address", address);
							intent.putExtra("name", name);
							intent.putExtra("currentService", currentService);
							intent.putExtra("currentJifen", currentJifen);
							intent.putExtra("merchanImg", merchanImg);
							intent.putExtra("status", 2);
							intent.putExtra("friend", true);
							intent.putExtra("who", ShareActivity.MY);
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 3) {
							String phone = tv_webSite_phone.getText().toString();
							String address = tv_webSite_address.getText().toString();
							String name = merchanName.getText().toString();
							Intent intent = new Intent(MerchantTurnActivity1.this, ShareActivity.class);
							intent.putExtra("phone", phone);
							intent.putExtra("address", address);
							intent.putExtra("name", name);
							intent.putExtra("currentService", currentService);
							intent.putExtra("currentJifen", currentJifen);
							intent.putExtra("merchanImg", merchanImg);
							intent.putExtra("status", 0);
							intent.putExtra("who", ShareActivity.MY);
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						} else if(which == 4) {
							String phone = tv_webSite_phone.getText().toString();
							String address = tv_webSite_address.getText().toString();
							String name = merchanName.getText().toString();
							Intent intent = new Intent(MerchantTurnActivity1.this, ShareActivity.class);
							intent.putExtra("phone", phone);
							intent.putExtra("address", address);
							intent.putExtra("name", name);
							intent.putExtra("currentService", currentService);
							intent.putExtra("currentJifen", currentJifen);
							intent.putExtra("merchanImg", merchanImg);
							intent.putExtra("status", 1);
							intent.putExtra("who", ShareActivity.MY);
							startActivity(intent);
							if(dialog != null) {
								dialog.dismiss();
							}
						}
					}
				});
				AlertDialog dialog = build.create();
				dialog.show();
			}
		});
		
		//商户图片，根据传递进来的图片地址进行下载图片  TODO
		merchan_image = (ImageView) findViewById(R.id.merchan_image);
		
		//商户的评分，根据传递进来的分数设置相对应的图片   TODO
		mer_rate_pf = (RatingBar) findViewById(R.id.mer_rate_pf);
		float f_score = Float.valueOf(score);
		mer_rate_pf.setRating(f_score);
		
		//排队的可能性，根据传递进来的参数设置，并根据条件显示提提颜色  TODO
		paidui_possible = (TextView) findViewById(R.id.paidui_possible);
		
		//商户地址,根据传递进来的参数进行设置  TODO
		tv_webSite_address = (TextView) findViewById(R.id.tv_webSite_address);
		
		//商户电话，根据传递进来的参数进行设置   TODO
		tv_webSite_phone = (TextView) findViewById(R.id.tv_webSite_phone);
		
		//商户电话 ,需要设置监听，点击时打电话   TODO
		bt_webSite_call = (RelativeLayout) findViewById(R.id.bt_webSite_call);
		bt_webSite_call.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if(!tv_webSite_phone.getText().toString().equals("暂无电话")) {
					
					new AlertDialog.Builder(MerchantTurnActivity1.this).setMessage("是否拨打电话：" + tv_webSite_phone.getText().toString())
					.setPositiveButton("确认", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent_call = new Intent(Intent.ACTION_CALL);
							intent_call.setData(Uri.parse("tel:" + tv_webSite_phone.getText().toString()));
							startActivity(intent_call);
						}
					})
					.setNegativeButton("取消", null).create().show();
				}
				
			}
		});
		
		//当前服务
		current_service = (LinearLayout) findViewById(R.id.current_service);
		//可以这里设置padding Left="20dp" Top="20dp" Right="20dp"  TODO
		
		current_service_name = (TextView) current_service.findViewById(R.id.tv_merchan_service);
		current_detail = (LinearLayout) current_service.findViewById(R.id.linear_merchan_detail_service);
		
		more_or_less = (ImageView) findViewById(R.id.more_or_less);
		
		scan_more = (RelativeLayout) findViewById(R.id.scan_more);
		scan_more.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(other_visiable) {
					other_visiable = false;
					AnimationController ac = new AnimationController();
					ac.fadeOut(other_service, 250, 0);
					RotateAnimation reverseAnimation = new RotateAnimation(-180, 0,
							RotateAnimation.RELATIVE_TO_SELF, 0.5f,
							RotateAnimation.RELATIVE_TO_SELF, 0.5f);
					reverseAnimation.setInterpolator(new LinearInterpolator());
					reverseAnimation.setDuration(250);
					reverseAnimation.setFillAfter(true);
					more_or_less.startAnimation(reverseAnimation);
					more_or_less.setImageResource(R.drawable.ic_arrow_down_black);
					int to = other_service.getTop();
					Log.i("bbb", to + " top");
					
				} else {
					other_visiable = true;
					AnimationController ac = new AnimationController();
					ac.fadeIn(other_service, 500, 0);
					RotateAnimation reverseAnimation = new RotateAnimation(-180, 0,
							RotateAnimation.RELATIVE_TO_SELF, 0.5f,
							RotateAnimation.RELATIVE_TO_SELF, 0.5f);
					reverseAnimation.setInterpolator(new LinearInterpolator());
					reverseAnimation.setDuration(250);
					reverseAnimation.setFillAfter(true);
					more_or_less.startAnimation(reverseAnimation);
					more_or_less.setImageResource(R.drawable.ic_arrow_up_black);
					int bo = other_service.getBottom();
					Log.i("bbb", bo + "bottom");
				}
			}
		});
		
		other_service = (LinearLayout) findViewById(R.id.other_service);
		
		bt_webSite_getRoute = (RelativeLayout) findViewById(R.id.bt_webSite_getRoute);
		
		//路线点击后跳转,通过current的坐标跳转
		bt_webSite_getRoute.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				Intent intent = new Intent(MerchantActivity.this, SiteRouteActivity.class);
				Intent intent = new Intent(MerchantTurnActivity1.this, RoutePlanActivity.class);
				if(current != null) {
//					intent.putExtra("latitude", 30.642237);
//					intent.putExtra("longtitude",114.292559);
					intent.putExtra("latitude", current.getPoint().getLatitudeE6());
					intent.putExtra("longtitude", current.getPoint().getLongitudeE6());
				}
				startActivity(intent);
			}
		});
		
		
		final TextView tv_first_id = (TextView) close_first.findViewById(R.id.site_id);
		TextView tv_first_siteName = (TextView) close_first.findViewById(R.id.site_name);
		TextView tv_first_sitePrice = (TextView) close_first.findViewById(R.id.site_price);
		TextView tv_first_siteDisatance = (TextView) close_first.findViewById(R.id.site_distance);
		RatingBar rate_bar_first = (RatingBar) close_first.findViewById(R.id.rate_bar);
		
		if(first != null) {
			float score = Float.valueOf(first.getScore());
			rate_bar_first.setRating(score);
			
			tv_first_id.setText(first.getId() + "");
			tv_first_siteName.setText(first.getSiteName());
			tv_first_sitePrice.setText(first.getMinServicePrice() + "~" + first.getMaxServicePrice());
			if(localCity) {
				tv_first_siteDisatance.setText(first.getSiteDistance() + "KM");
			} else {
				double dis = getDoublePoint2(first.getSiteDistance());
				if(dis > 50) {
					tv_first_siteDisatance.setText(" >50KM");
				} else {
					tv_first_siteDisatance.setText(dis + "KM");
				}
//				tv_first_siteDisatance.setText(">50KM");
			}
			close_first.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(MerchantTurnActivity1.this, MerchantTurnActivity1.class);
					intent.putExtra("siteID", tv_first_id.getText().toString());
					intent.putExtra("sitePosition", pre_position);
					intent.putExtra("serviceID", serviceID);
					
					if(ProApplication.slidingmenu) {
						//TODO  slidingmenu 模式
						Intent i = new Intent("com.my");
						i.putExtra("siteID", tv_first_id.getText().toString());
						i.putExtra("sitePosition", pre_position);
						intent.putExtra("serviceID", serviceID);
						sendBroadcast(i);
					} else {
						//TODO  直接跳转
						startActivity(intent);
						finish();
						overridePendingTransition(R.anim.slide_left, R.anim.slide_left_no);
					}
					
				}
			});
		}

		
		final TextView tv_second_id = (TextView) close_second.findViewById(R.id.site_id);
		TextView tv_second_siteName = (TextView) close_second.findViewById(R.id.site_name);
		TextView tv_second_sitePrice = (TextView) close_second.findViewById(R.id.site_price);
		TextView tv_second_siteDisatance = (TextView) close_second.findViewById(R.id.site_distance);
		RatingBar rate_bar = (RatingBar) close_second.findViewById(R.id.rate_bar);
		
		if(second != null) {
			
			tv_second_id.setText(second.getId() + "");
			tv_second_siteName.setText(second.getSiteName());
			tv_second_sitePrice.setText(second.getMinServicePrice() + "~" + second.getMaxServicePrice());
			if(localCity) {
				tv_second_siteDisatance.setText(second.getSiteDistance() + "KM");
			} else {
//				tv_second_siteDisatance.setText(">50KM");
				double dis = getDoublePoint2(second.getSiteDistance());
				if(dis > 50) {
					tv_second_siteDisatance.setText(" >50KM");
				} else {
					tv_second_siteDisatance.setText(dis + "KM");
				}
						
			}
			float score = Float.valueOf(second.getScore());
			rate_bar.setRating(score);
			
			close_second.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(MerchantTurnActivity1.this, MerchantTurnActivity1.class);
					intent.putExtra("siteID", tv_second_id.getText().toString());
					intent.putExtra("sitePosition", next_position);
					intent.putExtra("serviceID", serviceID);
					
					//TODO  slidingmenu 模式
					if(ProApplication.slidingmenu) {
						Intent i = new Intent("com.my");
						i.putExtra("siteID", tv_second_id.getText().toString());
						i.putExtra("sitePosition", next_position);
						intent.putExtra("serviceID", serviceID);
						sendBroadcast(i);
					} else {
						//直接跳转模式
						startActivity(intent);
						finish();
						overridePendingTransition(R.anim.slide_left, R.anim.slide_left_no);
					}
					
				}
			});
		}
		
		if(first == null && second == null) {
			image_line32.setVisibility(View.GONE);
			tv_other_site.setVisibility(View.GONE);
		}
		
	}

	public void exit(View view) {
		if(ProApplication.slidingmenu) {
			Intent intent = new Intent("com.my");
			intent.putExtra("modern", "menu");
			sendBroadcast(intent);
		} else {
			
			finish();
//			overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
		}
	}
	
	public double getDoublePoint2(String distance) {
		double d_dis = Double.valueOf(distance);
		long ll = Math.round(d_dis * 100);
		d_dis= ll / 100.0;
		return d_dis;
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		if(hasFocus) {
			if(scrollView != null) {
				scrollView.scrollTo(0, 0);  //滚动到顶部
			}
		}
		super.onWindowFocusChanged(hasFocus);
	}


	




	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
//			finish();
//			overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
			exit(null);
		}
		return super.onKeyDown(keyCode, event);
	}



	public static class DownLoadImage extends AsyncTask<String, Void, Void> {
		int myWidth;
		int myHeight;
		Bitmap bitmap;
		ImageView iv;
		
		public DownLoadImage(int myWidth,int myHeight,ImageView iv) {
			this.myWidth = myWidth;
			this.myHeight = myHeight;
			this.iv = iv;
		}


		@Override
		protected Void doInBackground(String... params) {
			String url = params[0];
			InputStream is = HttpUtil.getStream(url);
			//图片处理
			BitmapFactory.Options option = new BitmapFactory.Options();
            option.inJustDecodeBounds = true;
            
            if(is != null) {
            	bitmap = BitmapFactory.decodeStream(is,null,option);
            	int width = option.outWidth / myWidth;
            	if(width > 1) {
            		option.inSampleSize = width;
            	} else {
            		width = 1;
            	}
            	option.inJustDecodeBounds = false;
            	
            	InputStream is_1 = HttpUtil.getStream(url);
            	if(is_1 != null) {
            		bitmap = BitmapFactory.decodeStream(is_1, null, option);
            		Matrix matrix = new Matrix();
            		float f_yScale = ((float)myHeight) / option.outHeight;
            		float f_xScale = ((float)myWidth) /option.outWidth;
            		matrix.postScale(f_xScale, f_yScale);
            		if(bitmap != null) {
            			/*
            			bitmap = Bitmap.createBitmap(bitmap, 0, 0, option.outWidth, option.outHeight,matrix,true);
            			
            			ByteArrayOutputStream baos = new ByteArrayOutputStream();
            			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            			int quality = 100;
            			while(baos.toByteArray().length /1024 > 200) {  //大于200k就压缩
            				baos.reset();
            				bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
            				quality -= 2;
            			}
            			ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
            			bitmap = BitmapFactory.decodeStream(bais, null, null);*/
            			
            		}
            	}
            }
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			if(bitmap != null) {
				iv.setImageBitmap(bitmap);
			}
			super.onPostExecute(result);
		}
		
	}







	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onDestroy() {
		sh = false;
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		MobclickAgent.onResume(this);
		sh = false;
		super.onResume();
	}

	@Override
	protected void onPause() {
		MobclickAgent.onPause(this);
		super.onPause();
	}
	
/*	@Override
	public void toShare(int i) {
		if(wx.isInstall()) {
			boolean flag = false;
			if(wx.apiSupport()) {
				flag = true;
			}
			boolean success = wx.share("我在宽途网发现一家不错的店，快来看看吧：" + merchanName.getText().toString() + ";\n" + "地址：" + tv_webSite_address.getText().toString()
					+ ";\n" + "电话：" + tv_webSite_phone.getText().toString() + ";\n", null, flag);
		} else {
			showToast("您没有安装微信");
		}
	}*/

	
	
}
